package com.hemerasolutions.v2trainer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseGroupDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ExerciseGroupSearchCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.ExerciseGroupBeanInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseGroupDetailDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseGroupSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.ExerciseGroupServiceInterface;
import com.hemerasolutions.v2trainer.util.Utility;


@Controller
@ManagedBean(name="exerciseGroupBean")
@Scope("view")
public class ExerciseGroupBean extends BaseBean implements ExerciseGroupBeanInterface, ExerciseGroupDetailDelegateInterface  , ExerciseGroupSearchDelegateInterface {

	private static final long serialVersionUID = 1L;

	private List<VersionableExerciseGroup> versionableExerciseGroups;
	
	private ExerciseGroup exerciseGroup;

	private VersionableExerciseGroup versionableExerciseGroup;

	private VersionableExerciseGroup selVersionableExerciseGroup;
	
	private Exercise exercise;
	
	@Autowired
	private ExerciseDetailsCompInterface exerciseDetails;
	
	@Autowired
	private ExerciseGroupServiceInterface egService;
	
	@Autowired
	private ExerciseGroupSearchCompInterface exerciseGroupSearch;
	
	@Autowired
	private ExerciseGroupDetailsCompInterface exerciseGroupDetailsHandler;
	
	public ExerciseGroupBean() {
		super();
		
	}

	@PostConstruct
	public void init(){

		this.versionableExerciseGroups = new ArrayList<VersionableExerciseGroup>();
		this.exerciseGroupSearch.setDelegate(this);
		this.exerciseGroupSearch.refresh();
		this.exerciseGroupSearch.listAll();
		
	}


	public void toViewExercise(){
		this.exerciseDetails.setExercise(this.exercise);
		this.exerciseDetails.refresh();
	}
	
	public void createExerciseGroup() throws ExceptionService{
		try {
			this.exerciseGroup.setCreatedAt(Utility.getRoundedDate());
			this.exerciseGroup.setLastChange(Utility.getRoundedDate());
			this.exerciseGroup.setChangedBy(this.getActiveUser());

			egService.createVersionableExerciseGroup(versionableExerciseGroup);

			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheExerciseGroup") + " " +
					this.exerciseGroup.getName() + " " + 
					this.localizedText("wasCreatedWithSuccess"));			
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("CreateError"), e.getLocalizedMessage());
		}
		
	}

	public void saveExerciseGroup() throws ExceptionService{
		try {
			this.exerciseGroup.setLastChange(Utility.getRoundedDate());
			this.exerciseGroup.setChangedBy(this.getActiveUser());
			this.exerciseGroup.fillReferences();
			
			egService.saveExerciseGroup(exerciseGroup);

			this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
					this.localizedText("TheExerciseGroup") + " " +
					this.exerciseGroup.getName() + " " + 
					this.localizedText("wasSavedWithSuccess"));		
			
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}

	}
	
	public void deleteExerciseGroup() throws ExceptionService{
		try{
			egService.deleteExerciseGroup(this.exerciseGroup);
			this.showInfoMessage(	this.localizedText("Deleted"),
					this.localizedText("ExerciseGroupDeletedWithSuccess"));
			this.exerciseGroupSearch.refresh();
			this.exerciseGroup = null;
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"), e.getLocalizedMessage());
		}
	}
	
	
	public void toViewExerciseGroup(){
		this.exerciseGroup = this.versionableExerciseGroup.getExerciseGroup();
		this.exerciseGroupDetailsHandler.setVersionableExerciseGroup(this.versionableExerciseGroup);
		this.exerciseGroupDetailsHandler.setExerciseGroup(this.exerciseGroup);
		this.exerciseGroupDetailsHandler.setDelegate(this);
		this.exerciseGroupDetailsHandler.refresh();
	}
	
	public void cancelChange(){
		if(this.exerciseGroup.getId() != 0){
			this.exerciseGroup = egService.readExerciseGroup(exerciseGroup);
		}else{
			this.exerciseGroup = new ExerciseGroup();
		}
	}

	public void toAddExerciseGroup(){
		this.versionableExerciseGroup = new VersionableExerciseGroup();
		this.versionableExerciseGroup.addVersion();
	
		this.exerciseGroup = this.versionableExerciseGroup.getExerciseGroup();
		this.exerciseGroupDetailsHandler.setVersionableExerciseGroup(this.versionableExerciseGroup);
		this.exerciseGroupDetailsHandler.setExerciseGroup(this.exerciseGroup);
		this.exerciseGroupDetailsHandler.setDelegate(this);
		this.exerciseGroupDetailsHandler.refresh();
	
	}

	@Override
	public boolean onVersionableExerciseGroupSelected(
			VersionableExerciseGroup VersionableExerciseGroup) {
		//Nothing to do
		return false;
	}

	@Override
	public void onVersionableModalitySearchCompleted(List<VersionableExerciseGroup> versionableExerciseGroup) {
		this.versionableExerciseGroups = versionableExerciseGroup;
		
	}

	@Override
	public void onSaveVersionableExerciseGroup(
			VersionableExerciseGroup versionableExerciseGroup, ActionEvent ev) {
		this.exerciseGroup = versionableExerciseGroup.getExerciseGroup();
		this.saveExerciseGroup();
		
	}

	@Override
	public void onCreateVersionableExerciseGroup(
			VersionableExerciseGroup versionableExerciseGroup, ActionEvent ev) {
		this.versionableExerciseGroup = versionableExerciseGroup;
		this.exerciseGroup = versionableExerciseGroup.getExerciseGroup();
		this.createExerciseGroup();
	}

	@Override
	public void onVersionChanged(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}

	@Override
	public void onCancelChange(VersionableExerciseGroup versionableExerciseGroup) {
		this.cancelChange();
		
	}
	
	public List<VersionableExerciseGroup> getVersionableExerciseGroups() {
		return versionableExerciseGroups;
	}

	public void setVersionableExerciseGroups(
			List<VersionableExerciseGroup> versionableExercisesGroups) {
		this.versionableExerciseGroups = versionableExercisesGroups;
	}

	public VersionableExerciseGroup getSelVersionableExerciseGroup() {
		return selVersionableExerciseGroup;
	}

	public void setSelVersionableExerciseGroup(
			VersionableExerciseGroup selVersionableExerciseGroup) {
		this.selVersionableExerciseGroup = selVersionableExerciseGroup;
	}

	public ExerciseGroup getExerciseGroup() {
		return exerciseGroup;
	}

	public void setExerciseGroup(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}

	public VersionableExerciseGroup getVersionableExerciseGroup() {
		return versionableExerciseGroup;
	}

	public void setVersionableExerciseGroup(
			VersionableExerciseGroup versionableExerciseGroup) {
		this.versionableExerciseGroup = versionableExerciseGroup;
	}

	public ExerciseGroupSearchCompInterface getExerciseGroupSearch() {
		return exerciseGroupSearch;
	}

	public void setExerciseGroupSearch(
			ExerciseGroupSearchCompInterface exerciseGroupSearch) {
		this.exerciseGroupSearch = exerciseGroupSearch;
	}

	public ExerciseGroupDetailsCompInterface getExerciseGroupDetailsHandler() {
		return exerciseGroupDetailsHandler;
	}

	public void setExerciseGroupDetailsHandler(
			ExerciseGroupDetailsCompInterface exerciseGroupDetailsHandler) {
		this.exerciseGroupDetailsHandler = exerciseGroupDetailsHandler;
	}

	public Exercise getExercise() {
		return exercise;
	}

	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}

	public ExerciseDetailsCompInterface getExerciseDetails() {
		return exerciseDetails;
	}

	public void setExerciseDetails(ExerciseDetailsCompInterface exerciseDetails) {
		this.exerciseDetails = exerciseDetails;
	}

	
	
}
